<?php
/**
 * Created by PhpStorm.
 * User: 六花
 * Date: 2019/5/16
 * Time: 16:17
 */

class limited_settingApp extends BackendApp{


    var $_limited_mod;

    function __construct()
    {
        $this->limited_settingApp();
    }

    function limited_settingApp()
    {
        parent::BackendApp();
        $this->_limited_mod =& m('limited_setting');
    }

    function index()
    {
        ini_set('display_errors','on');
        error_reporting(E_ALL);
        if (!IS_POST)
        {
            $find_data = $this->_limited_mod->get('1=1');
            if (empty($find_data))
            {
                $this->show_warning('no_such_limitedtime');

                return;
            }
            if ($find_data['limited_url'])
            {
                $find_data['limited_url']  =   dirname(site_url()) . "/" . $find_data['limited_url'];
            }
//            $this->assign('limitedtime_list', $this->get_ad_type_list());
            /* 显示新增表单 */
            $yes_or_no = array(
                1 => Lang::get('yes'),
                0 => Lang::get('no'),
            );
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));
            $this->assign('yes_or_no', $yes_or_no);
            $this->assign('limited', $find_data);
            $this->display('limited_setting.form.html');
        }
        else


        {


            $find_data     = $this->_limited_mod->get('1=1');
            if (empty($find_data))
            {
                $this->show_warning('no_such_limitedtime');

                return;
            }

            //删除原图片
            if($_FILES['limited_url'] && $find_data['limited_url'] && $find_data['limited_url'] != $_FILES['limited_url']){
                if(file_exists(site_url() . '/' . $find_data['limited_url'])){
                    unlink(site_url() . '/' . $find_data['limited_url']);
                }
            }

//            site_url()) . '/' . $ad['limitedtime_url']
            $data = array(
                'limited_title' => $_POST['limited_title'],
            );
            $logo =   $this->_upload_logo($find_data['limited_id']);

            $logo && $data['limited_url'] = $logo;
            if ($logo === false)
            {
                return;
            }

            $rows=$this->_limited_mod->edit($find_data['limited_id'], $data);
            if ($this->_limited_mod->has_error())
            {
                $this->show_warning($this->_limited_mod->get_error());
                return;
            }else{
                $this->admin_log("修改手机广告[".$_POST['ad_name']."]",1);
            }

            $this->show_message('edit_limited_successed',
                'back_list',        'index.php?app=limited_setting&amp;act=index&amp;id='. $find_data['limited_id'],
                'edit_again',    'index.php?app=limited_setting&amp;act=index&amp;id=' . $find_data['limited_id']);
        }


    }



    function _upload_logo($limitedtime_id) {
        $file = $_FILES['limited_url'];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) { // 没有文件被上传
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES['limited_url']); //上传logo
        if (!$uploader->file_info()) {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=limited_setting&amp;act=edit&amp;id=' . $limitedtime_id);
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);

        /* 上传 */
        if ($file_path = $uploader->save('data/files/mall/limited', $limitedtime_id)) {   //保存到指定目录，并以指定文件名$ad_id存储
            return $file_path;
        } else {
            return false;
        }
    }

    function _upload_detail($limitedtime_id) {
        $file = $_FILES['detail_url'];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) { // 没有文件被上传
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES['detail_url']); //上传logo
        if (!$uploader->file_info()) {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=limitedtime_setting&amp;act=edit&amp;id=' . $limitedtime_id);
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);

        /* 上传 */
        if ($file_path = $uploader->save('data/files/mall/detail', $limitedtime_id)) {   //保存到指定目录，并以指定文件名$ad_id存储
            return $file_path;
        } else {
            return false;
        }
    }
}